﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace BuyThePeople.Models
{
    public class GoodsList
    {
        private List<T_GOODS> good;

        public List<T_GOODS> goods {
            get
            {
                return c < 0 ? null : good.GetRange(s, c);
            }
            set
            {
                this.good = value;
                if (value != null)
                {
                    foreach (T_GOODS g in value)
                    {
                        counts.Add(g.GoodsSeq, new DataModelsDataContext().T_JOINTPURCHASE.Count(x => x.GoodsSeq == g.GoodsSeq));
                    }
                }
            }
        }
        public Dictionary<int, int> counts { get; private set; }
        public int page { get; set; }
        public int pageMin { get; set; }
        public int pageMax { get; set; }
        public int pagePre { get; set; }
        public int pageNext { get; set; }
        public int pageCount { get; set; }
        public int itemCount { get; set; }
        public int showPageCount { get; set; }
        public int totalCount { get; set; }

        private int s;
        private int c;

        public GoodsList()
        {
            page = 1;
            itemCount = 12; // 4 * 3
            showPageCount = 10;
            counts = new Dictionary<int, int>();
        }

        public void CalcPage()
        {
            totalCount = good.Count();

            pageCount = int.Parse(Math.Ceiling((double)totalCount / (double)itemCount).ToString());
            pageMin = (int.Parse(Math.Floor((double)page / (double)showPageCount).ToString()) * 10) + 1;
            pageMax = (pageMin + showPageCount - 1 < pageCount ? pageMin + showPageCount - 1 : pageCount);
            pagePre = (pageMin - 1) <= 0 ? 1 : pageMin - 1;
            pageNext = (pageMax + 1) > pageCount ? pageCount : pageNext + 1;
                
            s = this.itemCount * (this.page - 1);
            c = this.totalCount - (s + this.itemCount) < 0 ? this.totalCount - s : this.itemCount;
        }
             
    }
}